extends Node

## 怪物掉落系统测试脚本
## 用于测试和验证怪物掉落功能的正确性

@onready var monster_manager = get_node("/root/MonsterManager")
@onready var item_manager = get_node("/root/ItemManager")

func _ready():
	"""初始化测试"""
	print("=== 怪物掉落系统测试开始 ===")
	
	# 等待管理器加载完成
	await get_tree().process_frame
	await get_tree().process_frame
	
	if monster_manager == null:
		print("错误：无法找到MonsterManager")
		return
	
	if item_manager == null:
		print("错误：无法找到ItemManager")
		return
	
	# 开始测试
	test_monster_drops()

func test_monster_drops():
	"""测试怪物掉落功能"""
	print("\n--- 开始测试怪物掉落 ---")
	
	# 获取所有怪物
	var all_monsters = monster_manager.get_all_monsters()
	if all_monsters.is_empty():
		print("错误：没有加载到怪物数据")
		return
	
	print("已加载 %d 个怪物" % all_monsters.size())
	
	# 测试每个怪物的掉落
	for monster in all_monsters:
		test_single_monster_drops(monster)
	
	# 测试等级掉落统计
	test_level_drop_statistics()
	
	print("\n=== 怪物掉落系统测试完成 ===")

func test_single_monster_drops(monster: Dictionary):
	"""测试单个怪物的掉落"""
	var monster_id = monster["id"]
	var monster_name = monster["name"]
	var monster_level = monster.get("level", 0)
	
	print("\n测试怪物: %s (ID: %s, 等级: %d)" % [monster_name, monster_id, monster_level])
	
	# 获取掉落配置信息
	var drop_info = monster_manager.get_monster_drop_info(monster_id)
	if drop_info.is_empty():
		print("  该怪物没有配置掉落")
		return
	
	# 显示掉落配置
	print("  掉落配置:")
	for category in ["equipment", "consumables", "materials"]:
		if drop_info.has(category):
			print("    %s:" % category)
			for item in drop_info[category]:
				print("      - %s (概率: %.1f%%)" % [item["id"], item["chance"]])
	
	# 模拟多次掉落测试
	var test_count = 100
	var drop_statistics = {}
	
	print("  进行 %d 次掉落模拟:" % test_count)
	for i in range(test_count):
		var drops = monster_manager.calculate_monster_drops(monster_id)
		for drop in drops:
			var key = "%s:%s" % [drop["type"], drop["id"]]
			if not drop_statistics.has(key):
				drop_statistics[key] = {"count": 0, "total_quantity": 0}
			drop_statistics[key]["count"] += 1
			drop_statistics[key]["total_quantity"] += drop["quantity"]
	
	# 显示统计结果
	if drop_statistics.is_empty():
		print("    没有掉落任何物品")
	else:
		print("    掉落统计:")
		for key in drop_statistics.keys():
			var stats = drop_statistics[key]
			var drop_rate = (stats["count"] * 100.0) / test_count
			var avg_quantity = float(stats["total_quantity"]) / stats["count"]
			print("      %s: %.1f%% (平均数量: %.1f)" % [key, drop_rate, avg_quantity])

func test_level_drop_statistics():
	"""测试等级掉落统计功能"""
	print("\n--- 测试等级掉落统计 ---")
	
	var available_levels = monster_manager.get_available_levels()
	for level in available_levels:
		var level_drops = monster_manager.get_drops_by_monster_level(level)
		print("\n等级 %d 怪物可掉落物品:" % level)
		
		for category in ["equipment", "consumables", "materials"]:
			if level_drops[category].size() > 0:
				print("  %s: %s" % [category, ", ".join(level_drops[category])])
			else:
				print("  %s: 无" % category)

func test_drop_validation():
	"""测试掉落物品的有效性"""
	print("\n--- 验证掉落物品有效性 ---")
	
	var all_monsters = monster_manager.get_all_monsters()
	var invalid_items = []
	
	for monster in all_monsters:
		var drop_info = monster_manager.get_monster_drop_info(monster["id"])
		if drop_info.is_empty():
			continue
		
		# 检查装备是否存在
		if drop_info.has("equipment"):
			for item in drop_info["equipment"]:
				if not item_manager.has_equipment(item["id"]):
					invalid_items.append("装备: %s (怪物: %s)" % [item["id"], monster["name"]])
		
		# 检查消耗品是否存在
		if drop_info.has("consumables"):
			for item in drop_info["consumables"]:
				if not item_manager.has_consumable(item["id"]):
					invalid_items.append("消耗品: %s (怪物: %s)" % [item["id"], monster["name"]])
		
		# 检查材料是否存在
		if drop_info.has("materials"):
			for item in drop_info["materials"]:
				if not item_manager.has_material(item["id"]):
					invalid_items.append("材料: %s (怪物: %s)" % [item["id"], monster["name"]])
	
	if invalid_items.is_empty():
		print("所有掉落物品都有效")
	else:
		print("发现无效的掉落物品:")
		for item in invalid_items:
			print("  - %s" % item)

func _input(event):
	"""处理输入事件"""
	if event.is_action_pressed("ui_accept"):
		test_monster_drops()
	elif event.is_action_pressed("ui_cancel"):
		test_drop_validation()
